package user;
import book.Library;
import constant.Constant;
import utils.PermissionException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-03
 * Time: 18:04
 */
public class ProxyUser {
    public User realuser;
    private Library library;
    public ProxyUser(User user){
        this.realuser = user;
        library = Library.getLibrary();
    }
    public User getRealuser(){
        return realuser;
    }
    //其他操作
//调⽤菜单
    public int display() {
        return this.realuser.display();
    }
    //--------------------------------管理员相关⽅法-----------------------------
    private void checkRealUserWhetherAdminUser(String msg){ //检查是否是管理员
        if(!(this.realuser instanceof AdminUser)){
            throw new PermissionException(msg); //不是管理员抛权限异常
        }
    }
    //添加书籍操作
    public void addBook() {
        System.out.println("代理类addBook方法执行了");
        try{
            checkRealUserWhetherAdminUser("普通用户没有权限添加书籍...");
        }catch(PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realuser)).addBook(); //去代理执行管理员类的添加书籍方法
    }
    //更新书籍操作
    public void updateBook() {
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限更新书籍...");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realuser)).updateBook();
    }
    //移除图书
    public void removeBook() {
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限移除书籍...");
        }catch (PermissionException e){
            e.printStackTrace();
        }
        ((AdminUser)(this.realuser)).removeBook();
    }
    //查看图书的借阅次数
    public void borrowCount( ) {
        System.out.println("代理类borrowCount方法执行了");

        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看借阅次数...");
        }catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realuser)).borrowCount();
    }
    //查看最受欢迎的前K本书
    public void generateBook() {
        System.out.println("代理类generateBook方法执行了");

        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看最受欢迎的前K本书...");
        }catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realuser)).generateBook();
    }
    //查看库存状态
    public void checkInventoryStatus() {
        System.out.println("代理类checkInventoryStatus方法执行了");
        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态...");
        }catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realuser)).checkInventoryStatus();
    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        System.out.println("代理类checkAndRemoveOldBooks方法执行了");

        try {
            checkRealUserWhetherAdminUser("普通用户没有权限查看库存状态...");
        }catch (PermissionException e) {
            e.printStackTrace();
        }

        ((AdminUser)(this.realuser)).checkAndRemoveOldBooks();
    }
    //--------------------------------普通相关⽅法-----------------------------
    private void checkRealUserWhetherNormalUser(String exceptionMessage) {
        if(!(this.realuser instanceof NormalUser)){ //检查是否是普通用户
            throw new PermissionException(exceptionMessage);
        }
    }
    //借阅图书
    public void borrowBook() {
        System.out.println("代理类borrowBook方法执行了");
        checkRealUserWhetherNormalUser("管理员不能借阅图书，请换做普通用户来借阅图书...");

        ((NormalUser)(this.realuser)).borrowBook();
    }
    //归还图书
    public void returnBook() {
        System.out.println("代理类returnBook方法执行了");
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书");
        ((NormalUser)(this.realuser)).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        System.out.println("代理类viewBorrowHistory方法执行了");
        checkRealUserWhetherNormalUser("管理员请以普通用户的方式归还图书");
        ((NormalUser)(this.realuser)).viewBorrowHistory();
    }

    public void handleOperation(int choice) {
        if(realuser instanceof AdminUser){
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                default:
                    break;
            }
        }else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowHistory();
                    break;
            }
        }
    }
}
